package top.tangyh.lamp.care.mapper;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import top.tangyh.basic.base.mapper.SuperMapper;
import top.tangyh.lamp.care.entity.BGenItem;
import org.springframework.stereotype.Repository;
import top.tangyh.lamp.care.vo.result.GenItemDto;
import top.tangyh.lamp.common.constant.CommonConstant;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * 通用项目配置表
 * </p>
 *
 * @author zqb
 * @date 2024-07-17 17:16:35
 * @create [2024-07-17 17:16:35] [zqb] [代码生成器生成]
 */
@Repository
public interface BGenItemMapper extends SuperMapper<BGenItem> {

    List<GenItemDto> findGenItemList(@Param("limitStart") Integer limitStart, @Param("pageSize") Integer pageSize, @Param(Constants.WRAPPER) Wrapper<BGenItem> wrapper);

    Long findGenItemListCount(@Param(Constants.WRAPPER) Wrapper<BGenItem> wrapper);

    default List<BGenItem> selectSysListByCodeAndModel(String code, String modelCode){
        LambdaQueryWrapper<BGenItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BGenItem::getItemCode,code)
                .eq(BGenItem::getModelCode,modelCode)
                .eq(BGenItem::getIsSys, CommonConstant.YES_NUMBER);
        return selectList(queryWrapper);
    }

    default void deleteByCodeAndModel(String code, String modelCode){
        LambdaQueryWrapper<BGenItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BGenItem::getItemCode,code)
                .eq(BGenItem::getModelCode,modelCode);
        delete(queryWrapper);
    }

    default List<BGenItem> queryItemByPidAndCode(String modeCode, String itemCodes,String patientIdent){
        LambdaQueryWrapper<BGenItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BGenItem::getModelCode,modeCode)
                .eq(BGenItem::getIsDeleted,CommonConstant.NO_NUMBER);
        if(StrUtil.isNotEmpty(patientIdent)){
            queryWrapper.eq(BGenItem::getIsSys,CommonConstant.NO_NUMBER);
            queryWrapper.eq(BGenItem::getPatientIdent,patientIdent);
        }
        else{
            queryWrapper.eq(BGenItem::getIsSys,CommonConstant.YES_NUMBER);
        }
        if(StrUtil.isNotEmpty(itemCodes)){
            List<String> codes = Arrays.asList(itemCodes.split(","));
            queryWrapper.in(BGenItem::getItemCode,codes);
        }
        List<BGenItem> list = selectList(queryWrapper);
        return list;
    }
}


